package com.tan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tan.entity.PageResult;
import com.tan.mapper.BrandMapper;
import com.tan.pojo.Brand;
import com.tan.service.IBrandService;
import com.tan.vo.BrandQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tanMax
 * @since 2022-09-30
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements IBrandService {
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public PageResult search(Integer currentPage, Integer pageSize, BrandQueryVo vo) {
        Page<Brand> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        if (vo!=null){
            if(!StringUtils.isEmpty(vo.getName())){
                wrapper.like("name",vo.getName());
            }
            if(!StringUtils.isEmpty(vo.getLetter())){
                wrapper.like("letter",vo.getLetter());
            }
        }
        Page<Brand> selectPage = brandMapper.selectPage(page, wrapper);
        return new PageResult(selectPage.getTotal(),selectPage.getRecords());

    }

    @Override
    public List selectByParentId() {
        QueryWrapper<Brand> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        List<Brand> brands = brandMapper.selectList(wrapper);
        return brands;
    }
}
